<template>
  <div>
    <div style="width: 600px; background: #fff; margin: 50px auto; padding: 30px 60px;">
      <h1 style="text-align: center; margin: 20px 0;">管理员登录</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'root01',
        password: '123456'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm() {
      let url = 'http://localhost:8090/admins/login';
      this.axios.post(url, this.ruleForm).then((response) => {
        console.log(response);
        if (response.data.result == 1) {
          // console.log('登录成功！');
          this.$message({
            message: '登录成功！',
            type: 'success'
          });
          this.$router.push('/index');
        } else if (response.data.result == 2) {
          // console.log('登录失败，密码错误！');
          this.$message.error('登录失败，密码错误！');
        } else {
          // console.log('登录失败，用户名错误！');
          this.$message.error('登录失败，用户名错误！');
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
body {
  background: #12487d;
}
</style>